package com.vieewer.btdemo.view.travel

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import com.bumptech.glide.Glide
import com.lindroid.androidutilskt.extension.getSp
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import com.vieewer.btdemo.bean.ChuanSceneBean
import com.vieewer.btdemo.common.Constants
import kotlinx.android.synthetic.main.activity_travel_detail.*

class TravelDetailActivity : BaseActivity() {


    private var stream_url = ""
    private var sh: SurfaceHolder? = null
    private var player: MediaPlayer? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_travel_detail)
        initViews()
        if (getSp(Constants.SP_KEY_TRAVEL_GUIDE_TYPE, "1") == "1") {
            Log.i("yjj", "导游播放语音模式")
            img_volume.setImageResource(R.drawable.icon_open_volume_h)
            playAudio()
        } else {
            img_volume.setImageResource(R.drawable.icon_open_volume_n)
        }



    }

    private fun playAudio() {
        if (stream_url != "") {
            player = MediaPlayer()
            try {
                player!!.setDataSource(this, Uri.parse(stream_url))
                sh = sv.holder
                sh!!.addCallback(MyCallBack())
                player!!.prepare()
                player!!.setOnPreparedListener {
                    player!!.start()
                    player!!.isLooping = true
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }


    private inner class MyCallBack : SurfaceHolder.Callback {
        override fun surfaceCreated(holder: SurfaceHolder) {
            player!!.setDisplay(holder)
        }

        override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

        }

        override fun surfaceDestroyed(holder: SurfaceHolder) {

        }
    }


    private fun initViews() {
        if (intent.getSerializableExtra("data") != null) {
            var data = intent.getSerializableExtra("data") as ChuanSceneBean.ScenceBeansBean
            if (data != null) {
                //Glide.with(this).load(data.scenceBean.pic).into(img_spot)
                txt_spot_description.text = "   " + data.scenceBean.description

                if (data.scenceBean.audio != null && data.scenceBean.audio != "") {
                    stream_url = data.scenceBean.audio
                    Log.i("yjj", "audio >>>>$stream_url")
                }
            }

        }

    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {

        Log.i("yjj", "TravelActivity onKeyUp >>>keyCode" + keyCode + ", action =" + event!!.action)

        when(keyCode) {
            119 -> {
                Log.i("yjj", "TravelActivity Fn按键对应键值为119")
            }
            141 -> {
                Log.i("yjj", "TravelActivity 触控长按按下对应键值为141")
            }

            142 -> {
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为142")
            }

            22 -> {
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为22")
                //scrollView.scrollY = 20
                scrollView.smoothScrollBy(0, 100)
            }
            21 -> {
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为21")
                //scrollView.scrollY = -20
                scrollView.smoothScrollBy(0, -100)

            }

        }

        return super.onKeyUp(keyCode, event)
    }


    override fun onDestroy() {
        super.onDestroy()


        if (player != null) {
            player!!.stop()
            player = null
        }

        if (sh != null) {
            sh = null
        }



    }








}